package com.dotflat.check.domain.C;

import lombok.Data;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

@Data
@XmlRootElement(name = "AttendanceRecord")  // 父节点名称
@XmlAccessorType(XmlAccessType.FIELD) //定义这个类中的何种类型需要映射到XML
public class AttendanceRecord {
    //考勤编号
    @XmlElement(name = "SAId")
    Integer SAId;
    // 考勤对象 幼儿1 员工2 家长3 userType
    @XmlElement(name = "AttendanceTaget")
    Integer AttendanceTaget;
    // 用户（幼儿/教师）编号
    @XmlElement(name = "UserId")
    Integer UserId = 0;

    //作用于放String UserId
    String UserIdV2;

    // 关联用户编号（监护人编号）
    @XmlElement(name = "RelateId")
    Integer RelateId;
    // 考勤卡编号
    @XmlElement(name = "SACardNo")
    String SACardNo;
    // 考勤日期
    @XmlElement(name = "SADate")
    String SADate;
    // 考勤进出方向全部ALL -1 未知Unknown 0 入园Enter 1 出园Leave 2
    @XmlElement(name = "AttendanceDirection")
    Integer AttendanceDirection;
    // 温度
    @XmlElement(name = "TemperatureField")
    Double TemperatureField;
    // 考勤方式 、手工 1、一体机读卡器 2、网络卡器 3、通道 4、门禁 5、手机 7、人脸
    @XmlElement(name = "CheckType")
    Integer CheckType;
    // 晨检类别
    @XmlElement(name = "DiseaseType")
    Integer DiseaseType;
    // 上传状态1已上传0未上传
    @XmlElement(name = "UploadState")
    Integer UploadState;


    public static List<AttendanceRecord> dom4jXml(String interfaceData) throws Exception {
        List<AttendanceRecord> attendanceRecordList =  new ArrayList();
        List<Element> elementList = null;
        AttendanceRecord beanAlarmHisList = null;

        try {
            SAXReader sr = new SAXReader();
            Document document = sr.read(new ByteArrayInputStream(interfaceData.getBytes()));

            Element root = document.getRootElement();
            elementList = root.elements();

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        for (Element e : elementList) {
            // 解析标签下一级标签
            Element e1 = e.element("AttendanceRecord");
            AttendanceRecord attendanceUser = new AttendanceRecord();
            attendanceUser.setUserId(Integer.valueOf(e1.elementText("UserId")));
            attendanceUser.setAttendanceTaget(Integer.valueOf(e1.elementText("AttendanceTaget")));
            attendanceUser.setSACardNo(e1.elementText("SACardNo"));
            attendanceUser.setSADate(e1.elementText("SADate"));
            attendanceUser.setAttendanceDirection(Integer.valueOf(e1.elementText("AttendanceDirection")));
            attendanceRecordList.add(attendanceUser);
        }
        return attendanceRecordList;
    }
}
